package org.dreamfly.core.test;

import org.apache.struts2.ServletActionContext;
import org.dreamfly.core.action.BaseAction;
import org.dreamfly.core.bean.RemoteServiceInfo;
import org.dreamfly.core.bean.SzPicture;
import org.dreamfly.core.remote.RuntimeServiceRegister;
import org.dreamfly.core.remote.ServiceRepository;
import org.dreamfly.core.remote.exception.ServiceException;
import org.dreamfly.core.service.PictureService;
import org.dreamfly.core.util.XMLReader;

import java.util.List;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: Gin
 * Date: 2010-11-14
 * Time: 11:24:06
 * To change this template use File | Settings | File Templates.
 */
public class testAction extends BaseAction {
    private Hessian hessianProxy;
    private RuntimeServiceRegister runtimeServiceRegister;
    private ServiceRepository serviceRepository;
    private PictureService pictureHessianProxy;
    /**/

    public String speak(){
        List<RemoteServiceInfo> infoList = XMLReader.readServerInfo(ServletActionContext.getServletContext().getRealPath("WEB-INF/picture.xml"));
      for(RemoteServiceInfo info : infoList){
        Class serviceClass = PictureService.class;
        String serviceId = info.getServerId();
        String serviceType = info.getServerType();
        String serviceUrl = info.getServerUrl();
          Properties props = null;

          runtimeServiceRegister.registService(serviceClass,serviceId,serviceType,serviceUrl,props);
      }    
          SzPicture picture = null;
        try {
            pictureHessianProxy = (PictureService) serviceRepository.getService(PictureService.class, "picture_1");
            picture = new SzPicture();
            picture.setTitle("%");
        } catch (ServiceException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        List<SzPicture> szPicturesResultSet = pictureHessianProxy.test(picture, 5L, 1L);
        if (szPicturesResultSet.size() == 0) {
            return "error";
        }
        return SUCCESS;
    }
    public String speakAgain(){
        try {
            hessianProxy = (Hessian)serviceRepository.getService(Hessian.class,"2");
            System.out.println(hessianProxy.callName("begin"));
            hessianProxy = (Hessian)serviceRepository.getService(Hessian.class,"1");
            System.out.println(hessianProxy.callName("begin"));
        } catch (ServiceException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return SUCCESS;
    }

    public String testPictureNameSearch() {
        SzPicture picture = null;
        try {
            pictureHessianProxy = (PictureService) serviceRepository.getService(PictureService.class, "picture_1");
            picture = new SzPicture();
            picture.setTitle("%");
        } catch (ServiceException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        /*List<SzPicture> list= pictureHessianProxy.normalSearch(picture, 5L, 1L);
        if (list.size() == 0) {
            return "error";
        }*/
        return SUCCESS;
    }
    
   public String search(){
       //TODO
       return null;
   }

    public void setPictureHessianProxy(PictureService pictureHessianProxy) {
        this.pictureHessianProxy = pictureHessianProxy;
    }

    public void setServiceRepository(ServiceRepository serviceRepository) {
        this.serviceRepository = serviceRepository;
    }

    public void setHessianProxy(Hessian hessianProxy) {
        this.hessianProxy = hessianProxy;
    }

    public void setRuntimeServiceRegister(RuntimeServiceRegister runtimeServiceRegister) {
        this.runtimeServiceRegister = runtimeServiceRegister;
    }
}
